﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhaoxi.CourseManagement.DataAccess.DataEntity;

namespace Zhaoxi.CourseManagement.DataAccess
{
    public class LocalDataAccess
    {
        private static LocalDataAccess instance;
        private LocalDataAccess() { }
        public static LocalDataAccess GetInstance()
        {
            return instance ?? (instance = new LocalDataAccess());
        }
        MySqlConnectionStringBuilder builder = null;
        MySqlConnection connection = null;
        MySqlCommand command = null;
        MySqlDataReader reader = null;

        private void Dispose()
        {
            if (reader != null)
            {
                reader.Close();
                reader = null;
            }
            if (command != null) 
            {
                command.Clone();
            }
            if (connection != null)
            {
                connection.Close();
            }
        }

        private bool DBConnection()
        {
            //从配置文件中获取数据库连接信息
            string connStr = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
            if (connection == null) 
            { 
                connection = new MySqlConnection(connStr);
            }
            try
            {
                connection.Open();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public UserEntity CheckUserInfo(string userName, string pwd)
        {
            try
            {
                if (DBConnection())
                {
                    string sql = "select * from user where user_name=@user_name and password=@pwd";
                    command = new MySqlCommand(sql, connection);
                    // 添加参数
                    command.Parameters.Add(new MySqlParameter("@user_name", MySqlDbType.VarChar) { Value = userName });
                    command.Parameters.Add(new MySqlParameter("@pwd", MySqlDbType.VarChar) { Value = pwd });

                    reader = command.ExecuteReader();

                    int count = reader.FieldCount;
                    if (count == 0)
                    {
                        return null;
                    }
                    while (reader.Read())
                    {
                        UserEntity userInfo = new UserEntity();
                        userInfo.UserName = reader.GetString(1);
                        userInfo.RealName = reader.GetString(2);
                        userInfo.Password = reader.GetString(3);
                        userInfo.Avatar = reader.GetString(4);
                        return userInfo;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.Dispose();
            }

            return null;
        }
    }
}
